package cn.hz.pi.api.util;

import java.text.SimpleDateFormat;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;


import cn.hz.pi.api.inteface.PIDate;
import cn.hz.pi.api.inteface.PiApi32;
import cn.hz.pi.api.inteface.PiApi64;
//import cn.hz.pi.inteface.PIDate;


/**
 * 获取时间
 * 
 * @author songxiaxia
 *
 */
public class PIDateUtil {

	private static final Logger log = LoggerFactory.getLogger(PIDateUtil.class);

	/**
	 * 将时间串转换为int 格式11-Aug-17 18:00:00 dd-mmm-yy hh:mm:ss
	 * 
	 * @param time
	 * @return
	 */
	public static int getPiTime(String time) {
		// 获取时间点
		String tim = time;
		if (time.length() > 10) {

			SimpleDateFormat dateFmat = new SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH);

			tim = dateFmat.format(DateUtil.fomatDate(time));
		}
		IntByReference timedata = new IntByReference();

		int flag = -1;

		try {
			flag = PiApi64.INSTANCE.pitm_parsetime(tim, 0, timedata);
		} catch (Throwable t) {
			try {
				flag = PiApi32.INSTANCE.pitm_parsetime(tim, 0, timedata);
			} catch (Throwable t2) {
				log.error("PI连接失败");
			}
		}

		if (0 == flag) {
			return timedata.getValue();
		}

		log.error("PI时间转化失败--> 时间：{}", time);

		return 0;
	}

	/**
	 * pi时间格式转标准时间格式(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param time
	 *            pi格式时间
	 * @return
	 */
	public static String formatePIDateToStandardTime(String time) {

		if (null == time || time.length() == 0) {
			return null;
		}

		try {
			SimpleDateFormat dateFmat = new SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH);
			return DateUtil.getFormatTime(dateFmat.parse(time));
		} catch (Exception e) {
			log.error("PI时间转化失败--> 时间：{}", time);
		}

		return null;
	}

	public static String getDateFromPiTime(int time) {

		// 获取时间点
		Pointer timestr = new Memory(19);

		try {
			PiApi64.INSTANCE.pitm_formtime(time, timestr, 19);
		} catch (Throwable t) {
			try {
				PiApi32.INSTANCE.pitm_formtime(time, timestr, 19);
			} catch (Throwable t2) {
				log.error("PI连接失败");
				return "";
			}
		}

		byte[] times = timestr.getByteArray(0, 19);
		StringBuffer buffer = new StringBuffer();
		for (byte b : times) {

			buffer.append((char) b);
		}

		return buffer.toString().trim();
	}

	/**
	 * 将PI日期转为数组型
	 * 
	 * @param time
	 * @return
	 */
	public int[] getTimeSecint(String time) {

		int[] time_arrays = new int[6];

		try {
			PiApi64.INSTANCE.pitm_secint(getTimeIntSec(time), time_arrays);
		} catch (Throwable t) {
			try {
				PiApi32.INSTANCE.pitm_secint(getTimeIntSec(time), time_arrays);
			} catch (Throwable t2) {
				log.error("PI连接失败");
			}
		}

		return time_arrays;
	}

	/**
	 * 将日期转为数组型，与pitm_secint互相逆
	 * 
	 * @param time
	 *            时间戳(格式:yyyy-MM-dd HH:mm:ss)
	 * @return
	 */
	public static Integer getTimeIntSec(String time) {
		return formatTimeToPI(time);
	}

	/**
	 * 将PI时间戳转化为系统默认格式(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param time
	 *            PI时间戳
	 * @return
	 */
	public static String formatPITime(Integer time) {
		return formatTimeFromPI(time);
	}

	/**
	 * 将PI时间戳转化为系统默认格式(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param time
	 *            PI时间戳
	 * @return
	 */
	public static String formatTimeFromPI(Integer time) {
		if (null == time) {
			return null;
		}
	  String	value = formatePIDateToStandardTime(getDateFromPiTime(time));
		return value;
	}

	/**
	 * 将指定时点转化为PI时间
	 * 
	 * @param time
	 *            时间戳(格式:yyyy-MM-dd HH:mm:ss)
	 * @return
	 */
	public static Integer formatTimeToPI(String time) {
		if (null == time || time.length() == 0) {
			return null;
		}

		String key = EhcacheConstants.piTimeToPI + ":" + time;
		
		 

		PIDate date = PIDate.getPIDate(time);

		int time_array[] = new int[] { date.getMonth(), date.getDay(), date.getYear(), date.getHour(),
				date.getMinutes(), date.getSeconds() };

		IntByReference rTime = new IntByReference();

		try {
			PiApi64.INSTANCE.pitm_intsec(rTime, time_array);
		} catch (Throwable t) {
			try {
				PiApi32.INSTANCE.pitm_intsec(rTime, time_array);
			} catch (Throwable t2) {
				log.error("PI连接失败");
				return null;
			}
		}

	

		return rTime.getValue();
	}

}
